unit SQLFilterFrm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxClasses, BaseFrm, StdCtrls, ExtCtrls, ImgList, StdActns, ActnList,
  cxStyles, cxGridBandedTableView, cxEdit, cxContainer,
  DialogBaseFrm,
  PoslovanjeSrv_TLB,
  DB, DBClient, alBaseDataSet, MConnect,
  kquerpnl, kdbstruc;

type
  TfrmSQLFilter = class(TfrmDialogBase)
    qrypnlMain: TKQueryPanel;
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    FSQLFilter: TStrings;
    function GetSQL: string;
  protected
  
  public
    constructor Create(AOwner: TComponent; ASession: ISession; ASQLFilter: TStrings; ADBStruct: TKDBStructure); overload;
    property SQL: string read GetSQL;
  end;

var
  frmSQLFilter: TfrmSQLFilter;

implementation

{$R *.dfm}

constructor TfrmSQLFilter.Create(AOwner: TComponent; ASession: ISession; ASQLFilter: TStrings; ADBStruct: TKDBStructure);
var
  i: Integer;
begin
  inherited Create(AOwner, ASession);
  if Assigned(ADBStruct) then
  begin
    with qrypnlMain do
    begin
      DbStruct := ADBStruct;
      for i := 0 to DbStruct.Fields.Count - 1 do
      begin
        if DbStruct.Fields[i].UseInResult then
        begin
          Qresult.ResultFields.AddBySQLExpr('ID', DbStruct.Fields[i].SQLExpr, stNoSort, 0)
        end;
      end;
    end;
  end;
  FSQLFilter := ASQLFilter;
  if Assigned(FSQLFilter) and (FSQLFilter.Count <> 0) then
    qrypnlMain.LoadFromStrings(FSQLFilter);
end;

procedure TfrmSQLFilter.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Assigned(FSQLFilter) and (ModalResult = mrOK) then
    qrypnlMain.SaveToStrings(FSQLFilter);
  inherited;
end;

procedure TfrmSQLFilter.FormShow(Sender: TObject);
begin
  inherited;
  qrypnlMain.Activate;
end;

function TfrmSQLFilter.GetSQL: string;
begin
  qrypnlMain.BuildQuery;
  Result := qrypnlMain.QResult.SQL.Text;
end;

end.
